package com.rtp.transformer.report.generator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import java.util.Random;

import jxl.Cell;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import bsh.EvalError;
import bsh.Interpreter;

public class DefaultGenerator implements IGenerator {

	private Interpreter i=new Interpreter();
	 
	
	public Object getFormulaResult(List input, String formula) {		
		Object result = null;
		try {
			i.set("input", input);
			i.eval("foo=" + formula);
			result = i.get("foo");

		} catch (EvalError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public void execute(List input,InputStream is,OutputStream os) {
		// TODO Auto-generated method stub
		WritableWorkbook wwb=null;		
		try {
			Workbook rwb = Workbook.getWorkbook(is);
			wwb = Workbook.createWorkbook(os, rwb);
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (WritableSheet ws : wwb.getSheets()) {
			for (int i = 0; i < ws.getRows(); i++) {
				for (Cell c : ws.getRow(i)) {
					String content = c.getContents();
					if (content.startsWith("$")) {
						String formula = content.substring(1, content.length());
						Object o = getFormulaResult(input, formula);
						if (o != null) {
							if (c instanceof Label) {
								Label l = (Label) c;
								l.setString(o.toString());
							}

						}
					}
					// System.out.println(content);
				}
			}
		}
		try {
			wwb.write();
			wwb.close();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setTemplate(String template) {
		
		URL url = this.getClass().getResource(template);
		String tmpfile="tmp";
		File tmp = new File(tmpfile);if(!tmp.exists())tmp.mkdir();
		File f = new File(tmpfile+"/"+new Random().toString()+".bsh");
		String source = f.getPath();
		byte[] b = null;
		try {
			InputStream is = url.openStream();
			b = new byte[is.available()];
			is.read(b);
			is.close();
			FileOutputStream fos = new FileOutputStream(new File(source));
			fos.write(b);
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			i.source(source);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EvalError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
